package com.ieds.web.service;

import com.ieds.core.dto.BootTablePage;
import com.ieds.core.mapper.KDatabaseMonitorDao;
import com.ieds.core.model.KDatabaseMonitor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: wanglei
 * @Date: 2020.05.25
 * @Description: com.ieds.web.service
 */
@Service
public class KDatabaseMonitorService {
    @Autowired
    private KDatabaseMonitorDao kDatabaseMonitorDao;

    /**
     * @param: [kDatabaseMonitor]
     * @return: void
     * @date: 2020.05.28 16:55
     * @Title：insert
     * @Description 插入数据源监控类
     */
    public void insert(KDatabaseMonitor kDatabaseMonitor){
        kDatabaseMonitorDao.insert(kDatabaseMonitor);
    }

    /**
     * @param: []
     * @return: java.lang.Integer
     * @date: 2020.05.28 16:56
     * @Title：getAllDatabaseMonitorSize
     * @Description 获取总数据源个数
     */
    public Integer getAllDatabaseMonitorSize() {
        return kDatabaseMonitorDao.getAllDatabaseMonitor().size();
    }

    /**
     * @param: [start, size, databaseType, databaseName]
     * @return: com.ieds.core.dto.BootTablePage
     * @date: 2020.05.29 11:18
     * @Title：getList
     * @Description 分页获取数据源列表
     */
    public BootTablePage getList(Integer start, Integer size, Integer databaseType, String databaseName) {
        KDatabaseMonitor template = new KDatabaseMonitor();
        template.setDatabaseType(databaseType);
        if(StringUtils.isNotEmpty(databaseName)){
            template.setDatabaseName(databaseName);
        }
        List<KDatabaseMonitor> kDatabaseMonitorList = kDatabaseMonitorDao.pageQuery(template, start, size);
        Long allCount = kDatabaseMonitorDao.allCount(template);
        BootTablePage bootTablePage = new BootTablePage();
        bootTablePage.setRows(kDatabaseMonitorList);
        bootTablePage.setTotal(allCount);

        return bootTablePage;
    }

    /**
     * @param: []
     * @return: java.lang.Integer
     * @date: 2020.05.29 11:26
     * @Title：getAllSuccess
     * @Description 获取总成功数
     */
    public Integer getAllSuccess() {
        List<KDatabaseMonitor> allDatabaseMonitor = kDatabaseMonitorDao.getAllDatabaseMonitor();
        int success = 0;
        if (CollectionUtils.isNotEmpty(allDatabaseMonitor)){
            for (KDatabaseMonitor kDatabaseMonitor : allDatabaseMonitor) {
                success += kDatabaseMonitor.getSuccessCount();
            }
        }
        return success;
    }

    /**
     * @param: []
     * @return: java.lang.Integer
     * @date: 2020.05.29 11:27
     * @Title：getAllFail
     * @Description 获取总失败次数
     */
    public Integer getAllFail() {
        List<KDatabaseMonitor> allDatabaseMonitor = kDatabaseMonitorDao.getAllDatabaseMonitor();
        int fail = 0;
        if (CollectionUtils.isNotEmpty(allDatabaseMonitor)){
            for (KDatabaseMonitor kDatabaseMonitor : allDatabaseMonitor) {
                fail += kDatabaseMonitor.getFailCount();
            }
        }
        return fail;
    }
}
